import {
  login,
  getUserInfo
} from '@/api/user/auth'
import { mchPage } from '@/api/mch'
import { userList } from '@/api/user'
import { agentFlowAlert } from '@/api/sp/flow'
import { MessageBox } from 'element-ui'
import router from '@/router'
import {
  getToken,
  setToken,
  removeToken
} from '@/utils/auth'

const state = {
  token: getToken(),
  agentFlow: {},
  loginInfo: null,
  userInfo: null,
  spInfo: null,
  fileBaseUrl: null,
  loaded: false,
  mchListOptions: [],
  userListOptions: [],
  userBanBtnList: [],
  authentication: null
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_USER_INFO: (state, loginInfo) => {
    state.loginInfo = loginInfo
    state.userInfo = loginInfo.userInfo
    state.userBanBtnList = loginInfo.banBtnList || []
    state.spInfo = loginInfo.spInfo
    state.fileBaseUrl = loginInfo.resBaseUrl
    state.loaded = true
  },
  SET_MCH_LIST: (state, list) => {
    state.mchListOptions = list
  },
  SET_USER_LIST: (state, list) => {
    state.userListOptions = list
  },
  SET_AGENT_FLOW: (state, data) => {
    state.agentFlow = data
  }
}

const actions = {
  // 登录
  Login({
    commit
  }, userInfo) {
    return new Promise((resolve, reject) => {
      userInfo.scope = 1
      login({ data: userInfo }).then(res => {
        sessionStorage.removeItem('exist_many_account')
        if (res.msg === 'exist_many_account') { // 控制多个相同账号，必须选择角色登录
          sessionStorage.setItem('exist_many_account', 'exist_many_account')
        }
        const token = res.data.token
        setToken(token)
        commit('SET_TOKEN', token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 流量预警信息
  agentFlowAlert({
    commit
  }, data) {
    return new Promise((resolve, reject) => {
      agentFlowAlert(data).then(res => {
        const agtFlowData = res.data
        // 流量余额
        if (agtFlowData.alertFlag == 1 && localStorage.getItem('flowWarnTag') == 'open') {
          MessageBox.confirm(agtFlowData.notice, '提示', {
            confirmButtonText: '去充值',
            cancelButtonText: '我知道了',
            type: 'warning'
          }).then(() => {
            router.push({
              path: '/system/flow-balance',
              query: {
                showPay: true
              }
            })
          })
          localStorage.setItem('flowWarnTag', 'close')
        }
        commit('SET_AGENT_FLOW', res.data)
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 获取用户信息
  GetUserInfo({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      getUserInfo().then(response => {
        const data = response.data
        commit('SET_USER_INFO', data)
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 获取商户列表
  fetchMchList({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      const data = {
        pageNum: 1,
        pageSize: 99999
      }
      mchPage({ data }).then(res => {
        const data = res.data.list || []
        commit('SET_MCH_LIST', data)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 获取用户列表
  fetchUserList({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      userList({ data: { includeAdmin: true } }).then(res => {
        const data = res.data || []
        commit('SET_USER_LIST', data)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 前端 登出
  FedLogOut({
    commit
  }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      removeToken()
      resolve()
    })
  }

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
